const install = function (Vue) {

    // 注意事项：
    // 打包上线之前在src/main.js中 改一下正确的host
    // Vue.prototype.$static = {
    // 目前不需要，做了一定的配置
    // dev 环境
    // api: 'http://10.22.4.115:19090'
    // build -- 测试环境
    // api: 'http://10.21.32.204:19090'

    // };
    // JSON序列化，默认带格式
    Vue.prototype.$log = (msg, formate) => {
        if (typeof formate !== 'undefined') {
            return JSON.stringify(msg);
        } else {
            return JSON.stringify(msg, null, 2);
        }
    };
    // 格式化时间
    Vue.prototype.g_time2date = (time, needhour, needsecond) => {
        if (time / 1 === 0 || isNaN(time)) {
            return '---';
        }
        time = Number(time)
        let date = new Date(time);
        let Y = date.getFullYear();
        let month = date.getMonth() + 1;
        let M = month < 10 ? ('0' + month) : month;
        let day = date.getDate();
        let D = day < 10 ? ('0' + day) : day;
        if (needhour) {
            let hour = date.getHours();
            let h = hour < 10 ? ('0' + hour) : hour;
            let minutes = date.getMinutes();
            let m = minutes < 10 ? ('0' + minutes) : minutes;
            if (needsecond) {
                let seconds = date.getSeconds();
                let s = seconds < 10 ? ('0' + seconds) : seconds;
                return Y + '-' + M + '-' + D + ' ' + h + ':' + m + ':' + s;
            } else {
                return Y + '-' + M + '-' + D + ' ' + h + ':' + m;
            }
        } else {
            return Y + '-' + M + '-' + D;
        }
    };
    // 数组去重
    Vue.prototype.g_makePureArr = (arr, name) => {
        if (!(arr instanceof Array)) {
            return [];
        }
        let obj = {};
        if (typeof name !== 'undefined') {
            arr.forEach((el) => {
                if (typeof el[name] !== 'undefined') {
                    obj['k' + el[name]] = el;
                }
            });
        } else {
            arr.forEach((el) => {
                obj['k' + el] = el;
            });
        }
        return Object.values(obj);
    };
    // 深拷贝
    Vue.prototype.g_cloneObj = (obj) => {
        let str;
        let newobj = obj.constructor === Array ? [] : {};
        if (typeof obj !== 'object') {
            return;
        } else if (window.JSON) {
            str = JSON.stringify(obj); // 序列化对象
            newobj = JSON.parse(str); // 还原
        } else {
            for (let i in obj) {
                newobj[i] = typeof obj[i] === 'object' ? Vue.prototype.g_cloneObj(obj[i]) : obj[i];
            }
        }
        return newobj;
    };
    // 空值监测
    Vue.prototype.g_isNull = (val) => {
        if (typeof val === 'undefined' || val === null || val === 'null' || val === '') {
            // 空的
            return true;
        } else {
            return false;
        }
    };
    // 正则test验证
    Vue.prototype.g_regCheck = (reg, data) => {
        return reg.test(data);
    };

    /**
     * 过滤器
     */
    Vue.filter('$f_formate', (str) => {
        if (/\/.*\//.exec(str) === null) {
            return str
        } else {
            return str.split(/\/.*\//.exec(str)[0])[1]
        }
    });

    Vue.filter('num', function (input) {
        if (typeof input !== 'number') {
            return '---'
        }
        if (input < 10) {
            return 'NO.0000' + input;
        }
        if (input < 100) { return 'NO.000' + input }
        if (input < 1000) { return 'NO.00' + input }
        if (input < 10000) { return 'NO.0' + input }
        if (input < 100000) { return 'NO.' + input }
    });
    Vue.filter('timeFormate', function (ms) {
        var currentdate,
            date = new Date(ms),
            cut1 = "-",
            cut2 = ":",
            objYear = date.getFullYear(),
            objMonth = date.getMonth() + 1,
            objDate = date.getDate(),
            objHours = date.getHours(),
            objMinutes = date.getMinutes(),
            objSeconds = date.getSeconds();
        if (objMonth >= 1 && objMonth <= 9) {
            objMonth = "0" + objMonth;
        }
        if (objDate >= 0 && objDate <= 9) {
            objDate = "0" + objDate;
        }
        if (objHours >= 0 && objHours <= 9) {
            objHours = "0" + objHours;
        }
        if (objMinutes >= 0 && objMinutes <= 9) {
            objMinutes = "0" + objMinutes;
        }
        if (objSeconds >= 0 && objSeconds <= 9) {
            objSeconds = "0" + objSeconds;
        }
        currentdate = objYear + cut1 + objMonth + cut1 + objDate + " " + objHours + cut2 + objMinutes + cut2 + objSeconds
        return currentdate;
    })

};
export default {
    install
};
